<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <meta name="Access-Control-Allow-Origin" content="/*">
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <!--验证码的css样式-->
    <link rel="stylesheet" href="css/verify.css">
    <script src="js/jquery.min.js" type="text/javascript"></script>
    <script src="js/bootstrap.min.js" type="text/javascript"></script>
    <script src="js/verify.min.js" type="text/javascript"></script>
	<title>梳碧湖驾校登陆系统</title>
	<script>
        $(function(){
            var vcode = false;
            var vo = null;
            $('#code').slideVerify({
                type : 1,		//类型
                vOffset : 5,	//误差量，根据需求自行调整
                barSize : {width:'100%',height:'40px'},
                success : function(obj) {
                    vcode = true;
                    vo = obj;
                },
                error : function() {
                    vcode = false;
                }
            });
            $("#btnLogin").click(function(){
                var uid = $("#uid>input").val();
                var pwd = $("#pwd>input").val();
                var reg = /^[0-9a-zA-Z]{4,8}$/;
                var role = $('input[name="optionsRadios"]:checked').val();

                if(uid){
                   $("#uid").removeClass("has-error");
                   $("#uid").addClass("has-success");
                }else{
                   $("#uid").removeClass("has-success");
                   $("#uid").addClass("has-error");
                   return;
                }
                if(reg.test(pwd)){
                    $("#pwd").removeClass("has-error");
                    $("#pwd").addClass("has-success");
                }else{
                    $("#pwd").removeClass("has-success");
                    $("#pwd").addClass("has-error");
                    return;
                }
                if(role==1){
                    if(vcode){
                        var ps = {
                            username:uid,
                            password:pwd
                        }
                        $.post("http://localhost:8889/admin/login",ps,function(data){
                            if(data){
                                //JSON.stringify() 方法是将一个JavaScript值(对象或者数组)转换为JSON字符串
                                var admin_id = data.id;
                                $.getJSON("http://localhost:8889/admin/find/"+admin_id,function (data) {
                                    var s = JSON.stringify(data[0]);
                                    localStorage.setItem("admin",s);
                                    console.log(s)
                                    location.href = "index.html";
                                })
                                //把数据存储在浏览器里面

                            }else{
                                vcode = false;
                                vo.refresh();
                                $(".msg").text("用户名或密码不正确，请重新输入！");
                                $(".msg").css("color","darkred");
                            }
                        });
                    }
                }else if(role==6){
                    if(vcode){
                        var ps = {
                            username:uid,
                            password:pwd
                        }
                        $.post("http://localhost:8889/student/login",ps,function(data){
                            if(data){
                                var id = data.stuId;
                                var role_id = id.toString();
                                console.log(role_id)
                                var info = {
                                    username:'xueyuan',
                                    password:'123456'
                                }
                                $.post("http://localhost:8889/admin/login",info,function(data){
                                    if(data){
                                        //JSON.stringify() 方法是将一个JavaScript值(对象或者数组)转换为JSON字符串
                                        var admin_id = data.id;
                                        $.getJSON("http://localhost:8889/admin/find/"+admin_id,function (data) {
                                            var s = JSON.stringify(data[0]);
                                            localStorage.setItem("admin",s);
                                            localStorage.setItem("role_id",role_id);
                                            location.href = "index.html";
                                        })
                                        //把数据存储在浏览器里面


                                    }else{
                                        vcode = false;
                                        vo.refresh();
                                        $(".msg").text("用户名或密码不正确，请重新输入！");
                                        $(".msg").css("color","darkred");
                                    }
                                });
                            }else{
                                vcode = false;
                                vo.refresh();
                                $(".msg").text("用户名或密码不正确，请重新输入！");
                                $(".msg").css("color","darkred");
                            }
                        })

                    }


                }else if(role==7){

                    if(vcode){
                        var ps = {
                            username:uid,
                            password:pwd
                        }
                        $.post("http://localhost:8889/coach/login",ps,function(data){
                            if(data){
                                var id = data.coaId;
                                var role_id = id.toString();
                                console.log(role_id)
                                console.log(data)
                                var info = {
                                    username:'jiaolian',
                                    password:'123456'
                                }
                                $.post("http://localhost:8889/admin/login",info,function(data){
                                    if(data){
                                        //JSON.stringify() 方法是将一个JavaScript值(对象或者数组)转换为JSON字符串
                                        var admin_id = data.id;
                                        $.getJSON("http://localhost:8889/admin/find/"+admin_id,function (data) {
                                            var s = JSON.stringify(data[0]);
                                            localStorage.setItem("admin",s);
                                            localStorage.setItem("role_id",role_id);
                                            console.log(s)
                                            location.href = "index.html";
                                        })
                                        //把数据存储在浏览器里面

                                    }else{
                                        vcode = false;
                                        vo.refresh();
                                        $(".msg").text("用户名或密码不正确，请重新输入！");
                                        $(".msg").css("color","darkred");
                                    }
                                });
                            }else{
                                vcode = false;
                                vo.refresh();
                                $(".msg").text("用户名或密码不正确，请重新输入！");
                                $(".msg").css("color","darkred");
                            }
                        })

                    }
                }

            })
        })
    </script>
    <style type="text/css" media="screen">
        .bg {
           background:url(images/login/bg.jpg) no-repeat center fixed;
           background-size:100%;
        }

    </style>
</head>


<body class="row bg">

	<div class="container">
        <div class="row" style="margin-top:180px">
            <div class="col-md-3"></div>
            <div class="col-md-6">
                <div class="panel" style="border:1px solid #9DB2f1">
                    <div class="panel-heading" style="background: #5d6b9a">

                        <h3 class="panel-title" style="color:white"><span class="glyphicon glyphicon-home" aria-hidden="true" style="margin-right:5px"></span>梳碧湖驾校管理员登录</h3>
                    </div>
                    <div class="panel-body">
                        <div class="form-group" id="uid">
                            <label style="font-size: 16px">账号</label>
                            <input class="form-control">
                        </div>
                        <div class="form-group" id="pwd">
                            <label style="font-size: 16px">密码</label>
                            <input type="password" class="form-control">
                        </div>
                        <div class="form-group"style="margin-top: 10px" id="role">
                            <label style="margin-right:20px;font-size: 16px">角色</label>
                            <label style="margin-right:30px;font-size: 16px">
                                <input type="radio" name="optionsRadios" id="optionsRadios1" value="1" checked> 管理员
                            </label>
                            <label style="margin-right:30px;font-size: 16px">
                                <input type="radio" name="optionsRadios" id="optionsRadios2" value="6"> 学员
                            </label>
                            <label style="margin-right:30px;font-size: 16px">
                                <input type="radio" name="optionsRadios" id="optionsRadios." value="7"> 教练
                            </label>
                        </div>
                        <div class="radio">

                        </div>
                        <div class="form-group">
                            <label>验证码</label>
                            <div id="code"></div>
                        </div>
                    </div>
                    <div class="panel-footer">
                        <div class="row">
                            <label class="col-md-10 msg"></label>
                            <div class="col-md-2">
                                <button id="btnLogin" class="btn btn-block" style="background: #5d6b9a;color: white ">登录</button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-md-3"></div>
        </div>
    </div>
</body>
</html>